/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.DAO;

import DBConnect.DBConnect;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author PhuongND
 */
public class CustomerDAO implements ICustomer{

    @Override
    public ArrayList<Customer> getListCustom() {     
        ArrayList<Customer> list = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        
        if(DBConnect.open()){
            try {
                ps = DBConnect.cnn.prepareStatement("select * from user_cus");
                rs = ps.executeQuery();
                list = new ArrayList<Customer>();
                int i=0;
                while (rs.next()) {  
                    i++;
                    list.add(new Customer(rs.getString("id"), rs.getString("username"), rs.getString("password"), rs.getString("email"), rs.getString("name"), rs.getString("address"), rs.getString("phone"), rs.getString("bank_code")));                    
                }
                //JOptionPane.showMessageDialog(null, "so ket qua: "+i);
            } catch (SQLException ex) {
                Logger.getLogger(CustomerDAO.class.getName()).log(Level.SEVERE, null, ex);
            }finally{            
                DBConnect.close(ps, rs);
            }                    
        }else{                    
            JOptionPane.showMessageDialog(null, "Sai cau hinh ket noi den server");
        }                
        return  list;
    }

    @Override
    public Customer addNew(Customer cus) {      
        PreparedStatement ps = null;
        if (DBConnect.open()) {
            try {
                ps = DBConnect.cnn.prepareStatement("INSERT INTO  `banhang`.`user_cus` (`id` ,`username` ,`password` ,`email` ,`name` ,`address` ,`phone` ,`bank_code`) VALUES (?,?,?,?,?,?,?,?)");
                ps.setString(1, cus.getId());
                ps.setString(2, cus.getUserName());
                ps.setString(3, cus.getPassword());
                ps.setString(4, cus.getEmail());
                ps.setString(5, cus.getName());
                ps.setString(6, cus.getAddress());
                ps.setString(7, cus.getIden_num());
                ps.setString(8, cus.getBank_code());
                if (ps.executeUpdate() > 0) {
                    System.out.print("ok");  //ok
                } else {
                    cus = null;
                }      
          } catch (SQLException ex) {
              JOptionPane.showMessageDialog(null, "Loi ket noi ");
          } finally {
                 DBConnect.close(ps);
          }        
      } else {
            cus = null;
      }
        return cus;
    }

    @Override
    public boolean update(String id) {
        throw new UnsupportedOperationException("Not supported yet.");               
    }

    @Override
    public boolean delete(String id) {       
        PreparedStatement ps = null; 
        if (DBConnect.open()) {
            try {
                ps = DBConnect.cnn.prepareStatement("delete from user_cus where id = ?");
                ps.setString(1,id);
                int rs = ps.executeUpdate();
       
                if(rs>0) {
                    return true;
                }
          } catch (SQLException ex) {
              JOptionPane.showMessageDialog(null, "Loi ket noi toi CSDL.");
          } finally {
                 DBConnect.close(ps);
          }        
        }else{                    
            JOptionPane.showMessageDialog(null, "Cau hinh ket noi toi CSDL sai.");
        }                
        return false;
    }

    public boolean checkID(String id) {
        PreparedStatement ps = null; 
        ResultSet rs = null;
        if (DBConnect.open()) {
            try {
                ps = DBConnect.cnn.prepareStatement("select * from user_cus where id = ?");
                ps.setString(1,id);
                
                rs = ps.executeQuery();                
                int i=0;
                if(rs.next()){
                    return false;
                }
          } catch (SQLException ex) {              
              JOptionPane.showMessageDialog(null, "Loi ket noi toi CSDL.");
              return false;
          } finally {
                 DBConnect.close(ps);
          }        
        }else{                    
            JOptionPane.showMessageDialog(null, "Cau hinh ket noi toi CSDL sai.");
            return false;
        }                
        return true;
    }
}
    
    

